using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace MapFileParser
{
    public struct Tile
    {
        public Int16 tileIndex;
        public Int16 specialIndex;
        public Tile(Int16 p1, Int16 p2)
        {
            tileIndex = p1;
            specialIndex = p2;
        }
    }
    public class Map
    {
        private Int16 width;
        private Int16 height;
        private List<Tile> tileArray = new List<Tile>();

        public Map(string fileName)
        {
            List<byte> byteArray = new List<byte>();
            tileArray = new List<Tile>();
            FileStream fs = File.OpenRead(fileName);
            BinaryReader br = new BinaryReader(fs);

            for (int i = 0; i < br.BaseStream.Length; i++)
            {
                byteArray.Add(br.ReadByte());
            }

            br.Close();
            fs.Close();

            width = ConvertBytePair(byteArray[0], byteArray[1]);
            height = ConvertBytePair(byteArray[2], byteArray[3]);

            for (int j = 4; j < byteArray.Count; j += 4)
            {
                tileArray.Add(new Tile(ConvertBytePair(byteArray[j], byteArray[j + 1]),
                    ConvertBytePair(byteArray[j + 2], byteArray[j + 3])));
            }
        }
        private Int16 ConvertBytePair(byte byte1, byte byte2)
        {
            return Convert.ToInt16(byte2.ToString() + byte1.ToString());
        }
        public Int16 Width
        {
            get { return width; }
        }
        public Int16 Height
        {
            get { return height; }
        }
        public List<Tile> TileArray
        {
            get { return tileArray; }
        }
        public int tileCount()
        {
            return tileArray.Count;
        }

    }
}
